import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import {workItemCategoryStatus} from '../Enum';
import stringify from 'Shared/utils/stringify';
import {
    InitData,
    WorkItems,
    WorkItemDetail,
    Categories,
    Part,
} from './DTO';
const baseUrl = '/afterSales/api/v1';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '维修项目'
});

const listDesc = formatMessage({
    id: 'api.message.listDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const detailDesc = formatMessage({
    id: 'api.message.detailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出{resourceName}'
}, {resourceName});

const updateLaborhourDesc = formatMessage({
    id: 'api.message.updateLaborhourDesc',
    defaultMessage: '编辑项目工时'
});

const resourceName2 = formatMessage({
    id: 'api.resourceName2',
    defaultMessage: '维修项目分类'
});

const categoryDesc = formatMessage({
    id: 'api.message.categoryDesc',
    defaultMessage: '查询{resourceName2}'
}, {resourceName2});

const addCategoryDesc = formatMessage({
    id: 'api.message.addCategoryDesc',
    defaultMessage: '新增{resourceName2}'
}, {resourceName2});

const partDesc = formatMessage({
    id: 'api.message.partDesc',
    defaultMessage: '查询服务站配件'
});
const assistDesc = formatMessage({
    id: 'api.message.assistDesc',
    defaultMessage: '编辑辅助维修项目'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/workItem/init`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, initQuery));
// 查询
export const getWorkItems = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/dealerWorkItems/forCurrentUser?${queryString}`, null, WorkItems)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, listDesc));
};

// 查询详情
export const getWorkItemDetail = id => request(`${baseUrl}/dealerWorkItems/${id}`, null, WorkItemDetail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, detailDesc));

// 导出
export const exportWorkItem = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/dealerWorkItems/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};
// 更新维修项目的工时信息
export const updateLaborhour = (id, data) => request(`${baseUrl}/dealerWorkItems/${id}/laborHour`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateLaborhourDesc));

// 更新维修项目对应的辅助维修项目
export const updateAssist = (id, data) => request(`${baseUrl}/dealerWorkItems/${id}/assistItems`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, assistDesc));

// 查询顶层维修分类
export const getCategoriesTop = () => request(`${baseUrl}/workItemCategories/topLevel/forCurrentUser`, null, Categories)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, categoryDesc));

// 查询维修分类
export const getCategories = id => request(`${baseUrl}/workItemCategories/${id}/children`, null, Categories)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, categoryDesc));

// 新增维修分类
export const addCategory = data => request(`${baseUrl}/workItemCategories`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addCategoryDesc));

// 查询服务站配件
export const getParts = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts/forDealerWorkItem?${queryString}`, null, Part)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partDesc));
};
// 查询有效的维修项目及工时
export const getWPC = queryCondition => {
    const queryString = stringify({
        ...queryCondition,
        status: workItemCategoryStatus.生效
    });
    return request(`${baseUrl}/dealerWorkItems/forCurrentUser?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, listDesc));
};
//导入材料工位模板
export const importLaborhourTemp = () => request(`${baseUrl}/dealerWorkItems/forPartItemImportTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);
//导入材料工位模板
export const importWorkItemTemp = () => request(`${baseUrl}/dealerWorkItems/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);
